package com.hl.vcd.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.hl.common.result.R;
import com.hl.common.utils.VideoTypeUtils;
import com.hl.vcd.entity.Video;
import com.hl.vcd.entity.vo.VideoSearch;
import com.hl.vcd.service.VideoService;
import org.apache.ibatis.annotations.Param;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.lang.Nullable;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.*;
import java.util.concurrent.TimeUnit;

/**
 * @author 朱炫达
 */
@RestController
@RequestMapping("/api/core")
public class VideoController {

    private VideoService videoService;
    @Autowired
    public void setVideoService(VideoService videoService){
        this.videoService = videoService;
    }

    private RedisTemplate<String,Object> redisTemplate;
    @Autowired
    public void setRedisTemplate(RedisTemplate<String,Object> redisTemplate){
        this.redisTemplate = redisTemplate;
    }

    @PostMapping("/add_video")
    public R addVideo(@RequestParam("image")MultipartFile imageFile,
                      @RequestParam("video")MultipartFile videoFile,
                      @RequestParam("userId")String userId,
                      @RequestParam("videoTitle")String videoTitle,
                      @RequestParam("type")Integer type,
                      @RequestParam("info")String info){
        Video video = new Video();
        video.setUserId(userId).setVideoTitle(videoTitle).setType(type).setInfo(info);
        boolean result = videoService.addVideo(video,imageFile,videoFile);
        if (result){
            return R.ok().message("上传视频成功");
        }
        else {
            return R.error().message("上传视频失败");
        }
    }

    @GetMapping("/get_main_top8x3")
    public R getMainTop8x3(){

        if (Boolean.FALSE.equals(redisTemplate.hasKey("main_top8"))){
            synchronized (this){
                if (Boolean.FALSE.equals(redisTemplate.hasKey("main_top8"))){
                    List<Video> videoList1 = videoService.getTopVideoByLike(8, VideoTypeUtils.ANIME);
                    List<Video> videoList2 = videoService.getTopVideoByLike(8,VideoTypeUtils.ENTERTAINMENT);
                    List<Video> videoList3 = videoService.getTopVideoByLike(8,VideoTypeUtils.SCIENCE_AND_TECHNOLOGY);

                    Map<String,Object> map1 = new LinkedHashMap<String,Object>(){};
                    Map<String,Object> map2 = new LinkedHashMap<String,Object>(){};
                    Map<String,Object> map3 = new LinkedHashMap<String,Object>(){};

                    map1.put("videoTitle","动漫");
                    map1.put("videoList",videoList1);
                    map2.put("videoTitle","娱乐");
                    map2.put("videoList",videoList2);
                    map3.put("videoTitle","科技");
                    map3.put("videoList",videoList3);

                    redisTemplate.opsForList().rightPushAll("main_top8",new ArrayList<>(Arrays.asList(map1,map2,map3)));
                    redisTemplate.expire("main_top8",1, TimeUnit.HOURS);
                }
            }
        }

        List<Object> allList = redisTemplate.opsForList().range("main_top8",0,-1);

        return R.ok().message("获取主视频列表成功").data(new HashMap<Object, Object>(){{put("main_top8", allList);}});
    }

    @GetMapping("/get_main_sidebar_top10")
    public R getMainSidebarTop10(){

        if (Boolean.FALSE.equals(redisTemplate.hasKey("sidebar_top10"))){
            synchronized (this){
                if (Boolean.FALSE.equals(redisTemplate.hasKey("sidebar_top10"))){
                    Map<Object, Object> temporaryMap;
                    List<Video> videoList = videoService.getTopVideoByPlay(10);
                    temporaryMap = new LinkedHashMap<Object, Object>() {};
                    temporaryMap.put("sidebar_top10",videoList);
                    redisTemplate.opsForHash().putAll("sidebar_top10",temporaryMap);
                    redisTemplate.expire("sidebar_top10",1, TimeUnit.HOURS);
                }
            }
        }

        Map<Object, Object> map = redisTemplate.opsForHash().entries("sidebar_top10");;

        return R.ok().message("获取侧边视频列表成功").data(map);
    }

    @GetMapping("/get_video_by_id/{id}")
    public R getVideoById(@PathVariable String id){
        Video video = videoService.getVideoById(id);
        Integer videoPlayNumber= (Integer) redisTemplate.opsForValue().get("playing" + id);
        if (videoPlayNumber != null && videoPlayNumber != 0){
            video.setPlayNumber(videoPlayNumber);
        }
        return R.ok().message("获取视频成功").data("video",video);
    }

    @GetMapping("/search_video")
    public R searchVideo(@Param("keyword") @Nullable String keyword,
                         @Param("type") @Nullable String type,
                         @Param("order") @Nullable String order,
                         @Param("current") @Nullable Integer current){
        if (current == null) current = 1;
        Page<VideoSearch> page = new Page<>(current,20);
        IPage<VideoSearch> videoPage = videoService.searchVideo(page, keyword, type, order);
        return R.ok().data("videoPage",videoPage);
    }

}
